package com.cdht.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * Map的中key转换为小写
 * @author 夜雨妖惜
 */
@SuppressWarnings({"rawtypes","unchecked"})
public class MapUtil {

    
    public static Map keyToLowerCase(Map map) {
        HashMap<String, Object> newMap = new HashMap<String, Object>();   
        if(map == null)return newMap;
        for (Object es : map.entrySet()) {
            Entry<String,Object> entry = (Entry<String, Object>) es;
            String key = entry.getKey();
            Object value = entry.getValue();
            if (!key.equals(key.toLowerCase())) {
                String newKey = key.toLowerCase();
                newMap.put(newKey, value);
            } else {
                newMap.put(key, value);
            }
        }
        return newMap;
    }
    
    
    public static List<Map<String, Object>>  keyToLowerCase(List<Map<String,Object>> list) {
        
        List<Map<String, Object>> newList = new ArrayList<>();
        //如果是空则直接返回
        if(list == null || list.isEmpty())return newList;
        for (Map<String, Object> hashMap : list) {
            Map<String, Object> newHashMap = new HashMap<>();
            for (Object es : hashMap.entrySet()) {
                Entry<String,Object> entry = (Entry<String, Object>) es;
                String key = entry.getKey();
                Object value = entry.getValue();
                if (!key.equals(key.toLowerCase())) {
                    String newKey = key.toLowerCase();
                    newHashMap.put(newKey, value);
                } else {
                    newHashMap.put(key, value);
                }
            }  
            newList.add(newHashMap); 
        }
        return newList;
    }
}
